package cn.com.elite;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.springframework.jdbc.core.JdbcTemplate;

import com.elitecrm.saas.service.EliteSAASException;


public class util{
	private static final Log log=LogFactory.getLog(util.class);
	public static String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw=new StringWriter();  
            e.printStackTrace(new PrintWriter(sw,true));  
            str=sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
	public static JdbcTemplate getJdbc(DataSource dataSource)throws Exception{
		JdbcTemplate jt=new JdbcTemplate(dataSource);
		return jt;
	}
	public static Document getDoc()throws Exception{
		Document result=DocumentFactory.getInstance().createDocument("utf-8");
		return result;
	}
	public static List QueryPersonUseExecute(String sql,int[] types,Object[] params,DataSource dataSource)throws Exception{
		JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
		List result=null;
		log.debug("sql:"+sql);
		try{
			result=jdbcTemplate.queryForList(sql,params,types);
		}catch(Exception e){
			 log.error("出错types："+getcontent1(types));
			 log.error("出错params："+getcontent(params));
			 log.error("出错SQL："+sql);
			 throw e;  
		}
		return result;
	}
	public static int UpdatePersonUseExecute(String sql,int[] types,Object[] params,DataSource dataSource)throws Exception{
		JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
		int result=0;
		log.debug("sql:"+sql);
		try{
			result=jdbcTemplate.update(sql,params,types);
		}catch(Exception e){
			 log.error("出错types："+getcontent1(types));
			 log.error("出错params："+getcontent(params));
			 log.error("出错SQL："+sql);
			 throw e;  
		}
		return result;
	}
	public static String getcontent(Object[] a){
    	String b="";
    	if(a!=null&&a.length>0){
    		for(int i=0;i<a.length;i++){
    			if(a[i]!=null){
    				b=b+"a["+i+"]："+a[i].toString()+"-";	
    			}else{
    				b=b+"a["+i+"]：null-";	
    			}
    		}
    	}
    	return b;
    }
	public static String getcontent1(int[] a){
    	String b="";
    	if(a!=null&&a.length>0){
    		for(int i=0;i<a.length;i++){
    			b=b+"a["+i+"]："+a[i]+"-";
    		}
    	}
    	return b;
    }
}
